package com.lry.crawlers.video.processor;

import com.lry.crawlers.util.ProcessVariable;
import com.lry.crawlers.video.entity.VideoVariable;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;
import java.util.regex.Pattern;

public class VideoUpdateProcessor implements PageProcessor {
    public Map<String,Integer> map;
    private Site site = Site.me()
            .setSleepTime(1)//采集间隔时间
            .setTimeOut(1000*10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数

    public VideoUpdateProcessor() {
    }
    public VideoUpdateProcessor(Map<String,Integer> map) {
        this.map = map;
    }

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();
        if(!url.contains("www.baidu.com")){
            //更新代码
            //视频章节更新
            VideoVariable videoVariable = new VideoVariable();
            for(VideoVariable fv : ProcessVariable.vVariable.values()){
                if(url.contains(fv.getUrl())){
                    videoVariable = fv;
                }
            }
                //获取视频内容信息：content
                //获取视频章节信息：chapterTitle，chapterUrl，chapterId
                String[] chapterTitles = videoVariable.getChapterTitle().split("-!-");
                String[] chapterUrls = videoVariable.getChapterUrl().split("-!-");
                String[] updateTimes = videoVariable.getUpdateTime().split("-!-");
                List<String> chapterTitle = page.getHtml().xpath(chapterTitles[0]).regex(chapterTitles.length !=1 ? chapterTitles[1] : ".+").all();
                List<String> chapterUrl = page.getHtml().xpath(chapterUrls[0]).regex(chapterUrls.length !=1 ? chapterUrls[1] : ".+").all();
                List<String> updateTime = page.getHtml().xpath(updateTimes[0]).regex(updateTimes.length !=1 ? updateTimes[1] : ".+").all();
            if(chapterTitles.length ==3){
                String[] splitTitl = chapterTitles[2].split(",");
                if(splitTitl.length ==1){
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),chapterTitle.size());
                }else{
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),Integer.parseInt(splitTitl[1]));

                }
            }
            if(chapterUrls.length == 3){
                String[] splitUrls = chapterUrls[2].split(",");
                if(splitUrls.length ==1){
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),chapterUrl.size());
                }else{
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),Integer.parseInt(splitUrls[1]));
                }
            }
                String videoId = Pattern.compile("[^0-9]").matcher(url).replaceAll("").trim();
                //获取视频章节与内容的对应的chapterId
                //获取现在的章节数量
                int xsize = chapterUrl.size();
                //获取原本的章节数量
                int size = map.get(url);
                if(xsize>size){
                    //创建储存Map数据，用于业务处理
                    Map<String,Object> map = new HashMap<>();
                    map.put("key","chapter");
                    map.put("chapterTitle",chapterTitle.subList(size,xsize));
                    map.put("chapterUrl", chapterUrl.subList(size,xsize));
                    map.put("videoId",videoId);
                    map.put("updateTime",updateTime.get(0));
                    map.put("chapterNum",xsize);
                    map.put("updateUrl",url);
                    map.put("id", videoVariable.getId());
                    //提交数据，给Pipeline处理
                    page.putField("data",map);
                }
            }

        else{
            page.addTargetRequests(new ArrayList<>(map.keySet()));
        }

    }

    @Override
    public Site getSite() {
        return site;
    }

}
